package fr.paulmasson.jfreex.db.dao;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.List;

import fr.paulmasson.jfreex.db.DbManager;
import fr.paulmasson.jfreex.db.bo.BoChannel;
import fr.paulmasson.jfreex.db.bo.BoProvider;
import fr.paulmasson.jfreex.utils.Common;
import fr.paulmasson.jfreex.utils.Wget;

public class DaoChannel {

	public static void setup() {
		if (isEmpty()) {
//			try {
//				String data = Wget.wget(Common.providersUrl);
//				String[] data_lines = data.split("\n");
//			
//				for (String line : data_lines) {
//					if (line.length() > 0) {
//						String[] infos = line.split("	");
//						BoProvider prov = new BoProvider();
//						prov.setName(infos[0]);
//						prov.setChannelUrl(infos[1]);
//						prov.setChannelUrlMirror(infos[2]);
//						DbManager.saveEntity(prov);
//					}
//				}
//			} catch (MalformedURLException e) {
//				e.printStackTrace();
//			} catch (IOException e) {
//				e.printStackTrace();
//			}
		}
	}
	
	public static int count() {
		return DbManager.get("from BoChannel").size();
	}
	
	public static boolean isEmpty() {
		return count()==0;
	}

	public static BoChannel getBoChannel(int id) {
		List<Object> list = DbManager.get("from BoChannel where id="+id+"");
		if (list.size()==1)
			return (BoChannel) list.get(0);
		else
			return null;
	}
	
	public static void printTable(List<Object> data) {
		Dao.printTable(data);
	}

	public static String toFormattedString() {
		List<Object> list = DbManager.get("from BoChannel");
		list.add(0,new BoChannel());
		printTable(list);
		return "";
	}
	
}

